package com.example.management.service.impl;

import com.example.management.mapper.DormitoryMapper;
import com.example.management.pojo.Dormitory;
import com.example.management.service.DormitoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DormitoryServiceImpl implements DormitoryService {
    @Autowired
    private DormitoryMapper dormitoryMapper;

    @Override
    public List<Dormitory> getAllDormitory() {
        return dormitoryMapper.getAllDormitory();
    }

    @Override
    public String changeDormitory(Dormitory dormitory) {
        try {
            if (dormitoryMapper.selectDormitory(dormitory) == null) {
                return "查无此宿舍!";
            } else {
                dormitoryMapper.changeDormitory(dormitory);
                return "修改成功！";
            }
        } catch (Exception e) {
            return "Error!";
        }
    }

    @Override
    public String deleteDormitory(Integer number) {
        Dormitory dormitory = new Dormitory();
        dormitory.setNumber(number);
        try {
            if (dormitoryMapper.selectDormitory(dormitory) == null) {
                return "查无此宿舍!";
            } else {
                dormitoryMapper.deleteByNumber(dormitory);
                return "删除编号为" + number + "的宿舍成功!";
            }
        } catch (Exception e) {
            return "Error!";
        }
    }

    @Override
    public String addDormitory(Dormitory dormitory) {
        try {
            if (dormitoryMapper.selectDormitory(dormitory) != null) {
                return "该宿舍已经存在!";
            } else {
                dormitoryMapper.insertDormitory(dormitory);
                return "添加宿舍成功!";
            }
        } catch (Exception e) {
            return "Error!";
        }
    }
}
